package com.lxl.priv.zip;

//import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipDemo {
    public static void main(String[] args) {
        String zipFilePath = "D:\\Desktop\\abcd.zip";
        String destDir = "D:\\";

        try {
            File destDirFile = new File(destDir);
            if (!destDirFile.exists()) {
                destDirFile.mkdirs();
            }

            FileInputStream fis = new FileInputStream(zipFilePath);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry zipEntry = zis.getNextEntry();
            String filePath = "";
            while (zipEntry != null) {
                filePath = destDir + zipEntry.getName();
                if (zipEntry.isDirectory()) {
                    File dir = new File(filePath);
                    dir.mkdirs();
                } else {
                    if (new File(filePath).exists()) {
                        new File(filePath).delete();
                    }
                    FileOutputStream fos = new FileOutputStream(filePath);
                    byte[] buffer = new byte[1024];
                    int len;
                    while ((len = zis.read(buffer)) > 0) {
                        fos.write(buffer, 0, len);
                    }
                    fos.close();
                }
                zipEntry = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
            fis.close();
            if (new File(filePath + ".csv").exists()) {
                new File(filePath + ".csv").delete();
            }
            //FileUtils.moveFile(new File(filePath), new File(filePath + ".csv")); // 将压缩包重命名为csv文件
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*String inputFilePath = "input.txt";
        String outputFilePath = "output.csv";

        try {
            FileReader fileReader = new FileReader(inputFilePath);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            FileWriter fileWriter = new FileWriter(outputFilePath);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                String[] values = line.split(" ");
                String csvLine = String.join(",", values);
                bufferedWriter.write(csvLine);
                bufferedWriter.newLine();
            }

            bufferedReader.close();
            bufferedWriter.close();
            fileReader.close();
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
